package com.briup.myblog.web.controller;


import com.briup.myblog.aop.Logging;
import com.briup.myblog.bean.basic.BaseUser;
import com.briup.myblog.service.IBaseUserService;
import com.briup.myblog.utils.Result;
import com.briup.myblog.utils.ResultUtil;
import com.briup.myblog.web.vm.UserInsertVM;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "登录模块")
@RestController
public class LoginController {
    @Autowired
    private IBaseUserService service;
    @ApiOperation("登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名",required = true),
            @ApiImplicitParam(name = "password",value = "密码",required = true)
    })
    @PostMapping("/login")
    public Result login(String username,String password){
        String token = service.login(username,password);
        return ResultUtil.success(token);
    }
    @ApiOperation("注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dob",value = "出生日期",required = true),
            @ApiImplicitParam(name = "email",value = "电子邮箱",required = true),
            @ApiImplicitParam(name = "gender",value = "性别",required = true),
            @ApiImplicitParam(name = "icon",value = "头像url",required = true),
            @ApiImplicitParam(name = "password",value = "密码",required = true),
            @ApiImplicitParam(name = "realname",value = "真实姓名",required = true),
            @ApiImplicitParam(name = "telephone",value = "电话号码",required = true),
            @ApiImplicitParam(name = "username",value = "用户名",required = true),
    })
    @PostMapping("/register")
    public Result addUser(UserInsertVM vm){
         BaseUser user = new BaseUser();
         user.setDob(vm.getDob());
         user.setEmail(vm.getEmail());
         user.setGender(vm.getGender());
         user.setIcon(vm.getIcon());
         user.setPassword(vm.getPassword());
         user.setRealname(vm.getRealname());
         user.setTelephone(vm.getTelephone());
         user.setUsername(vm.getUsername());
         service.addUser(user);
         BeanUtils.copyProperties(vm,user);
         return ResultUtil.success("注册成功，请登录");
    }
    @ApiOperation("退出登录")
    @PostMapping("/logout")
    public Result logout(HttpServletRequest request){
        return ResultUtil.success("退出登录成功，请重新登录");
    }



}
